#include "reg52.h"
#include "iic.h"

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;

unsigned char dat,dat1;
bit flag,flag1,flag2,flag3;
unsigned char light[10];
code unsigned char Seg_Table[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e, //F
0xbf,
0x91	
};

code unsigned char Seg_Table1[] = 
{
0x40, //0
0x89, //1
0x24, //2
0x30, //3
0x19, //4
0x12, //5
0x02, //6
0x88, //7
0x00, //8
0x10, //9
0x08, //A
0x03, //b
0x46, //C
0x21, //d
0x06, //E
0x0e, //F
0xbf	
};

void InitHC573(unsigned char channel)
{
	switch(channel)
	{
		case 4:
		P2 = (P2 & 0x1f) | 0x80;
		break;
		case 5:
		P2 = (P2 & 0x1f) | 0xa0;
		break;
		case 6:
		P2 = (P2 & 0x1f) | 0xc0;
		break;
		case 7:
		P2 = (P2 & 0x1f) | 0xe0;
		break;
		case 0:
		P2 = (P2 & 0x1f) | 0x00;
		break;		
	}
	
}

void SMG_Bit(unsigned char pos,unsigned dat)
{
	InitHC573(6);
	P0 = 0x01<<pos;
	InitHC573(7);
	P0 = dat;
	InitHC573(0);
}

void SMGALL(unsigned char dat)
{
	InitHC573(6);
	P0 = 0xff;
	InitHC573(7);
	P0 = dat;
	InitHC573(0);
}
	
void Delay(unsigned int t)
{
	while(t--);
}



void GetLight()
{
	dat1 = ReadAD(0x41)/51;
	dat = ReadAD(0x43)/51;
	

	light[0] = (unsigned char )dat;
	light[1] = (dat*100) /10%10;
	light[2] = (dat*100)%10;
	
	light[0] = (unsigned char )dat1;
	light[1] = (dat1*100) /10%10;
	light[2] = (dat1*100)%10;
}



void SMGlight(unsigned char k)
{
	switch(k)
	{
		case 0:
		SMG_Bit(7,Seg_Table1[light[2]]);
		Delay(100);
		SMG_Bit(6,Seg_Table[light[1]]);
		Delay(100);
		SMG_Bit(5,Seg_Table[light[0]]);
		Delay(100);
		SMG_Bit(1,Seg_Table[17]);
		Delay(100);		
		SMGALL(0xff);
		break;
		
		case 1:
		SMG_Bit(7,Seg_Table1[light[5]]);
		Delay(100);
		SMG_Bit(6,Seg_Table[light[4]]);
		Delay(100);
		SMG_Bit(5,Seg_Table[light[3]]);
		Delay(100);
		SMG_Bit(1,Seg_Table[15]);
		Delay(100);		
		SMGALL(0xff);
		break;
	}
}

void LED()
{
	InitHC573(4);
	if(flag == 0)
	{
		L1 = 1;
		L2 = 0;
	}
	else
	{
			L1 = 0;
			L2 = 1;
	}
	
	if(dat<76 || (dat>127) && (dat < 188))
	{
		L3 = 1;
	}
	else
	{
		L3 = 0;
	}
	
	if(flag1 == 0)
		L4 = 1;
	else
		L4 = 0;
}
void DelaySMG(unsigned int t,bit k)
{
	while(t--)
	{
			SMGlight(k);
	}	

}
void ScanKey()
{
		if(S4 == 0)
	{
		DelaySMG(100,0);
		if(S4 == 0)
		{
			if(flag == 0)
			{
				SMGlight(0);
				flag = 1;
			}
			else
			{
				flag = 0;
				SMGlight(1);
			}
			while(S4 == 0);
		}
	}
	
	if(S5 == 0)
	{
		Delay(100);
		if(S5 == 0)
		{
			if(flag1 == 0)
			{
			dat = 102;
			flag1 = 1;

		}
		else
		{
			dat = dat1;
			flag1 = 0;
		}
			while(S5 == 0);
	}
	}
	
	if(S6 == 0)
	{
		Delay(100);
		if(S6 == 0)
		{
			if(flag2 == 0)
				flag2 = 1;
			else
				flag2 = 0;
		while(S6 == 0);	
		}
	}
	
		if(S7 == 0)
	{
		Delay(100);
		if(S7 == 0)
		{
			if(flag3 == 0)
				flag3 = 1;
			else
				flag3 = 0;
		while(S7 == 0);	
		}
	}
}

void main()
{
	while(1)
	{
		GetLight();
		ScanKey();

	}
}




























